package com.wwj.tem.service.impl;

import cn.hutool.extra.mail.MailUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.tem.constant.Constants;
import com.wwj.tem.exception.BusinessException;
import com.wwj.tem.exception.GlobalExceptionHandler;
import com.wwj.tem.model.entity.SysUser;
import com.wwj.tem.model.vo.SysUserVo;
import com.wwj.tem.service.SysUserService;
import com.wwj.tem.mapper.SysUserMapper;
import com.wwj.tem.utils.BeanCopyUtils;
import com.wwj.tem.utils.RedisUtils;
import com.wwj.tem.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Home
 * @description 针对表【sys_user】的数据库操作Service实现
 * @createDate 2024-07-28 17:25:20
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
	
	@Resource
	private SysUserMapper sysUserMapper;
	
	@Resource
	private RedisUtils redisUtils;
	
	public List<SysUserVo> all() {
		List<SysUser> sysUserList = sysUserMapper.selectList(null);
		List<SysUserVo> sysUserVoList = BeanCopyUtils.copyBeanList(sysUserList, SysUserVo.class);
		return sysUserVoList;
	}
	
	public void sendEmailCode(String email, Integer type) {
		/**
		 * type 0 注册 1 重置密码
		 */
		// 参数校验
		if (StringUtils.isEmpty(email)) {
			throw new BusinessException("邮箱地址不能为空");
		}
		if (type == null || (type != 0 && type != 1)) {
			throw new BusinessException("无效的操作类型");
		}
		String theme;
		String redisKey;
		// 生成验证码
		String contentText = StringUtils.randomString(Constants.EMAIL_CODE_LENGTH);
		
		if (type == 0) {
			theme = "注册账号";
			redisKey = Constants.EMAIL_CODE_REGISTER + email;
		} else {
			theme = "重置密码";
			redisKey = Constants.EMAIL_CODE_RESET + email;
		}
		// 存储验证码
		redisUtils.setWithExpire(redisKey, contentText, Constants.EMAIL_EXPIRATION, TimeUnit.MINUTES);
		MailUtil.send(email, theme, scopeTemplate(contentText), true);
	}
	
	public static String scopeTemplate(String contentText) {
		
		String filename = "template.html";
		InputStream inputStream = ClassLoader.getSystemResourceAsStream(filename);
		BufferedReader fileReader = new BufferedReader(new InputStreamReader(inputStream));
		StringBuffer buffer = new StringBuffer();
		
		String line = "";
		try {
			while ((line = fileReader.readLine()) != null) {
				buffer.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("read template");
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (fileReader != null) {
				try {
					fileReader.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		//落款处需要时间则加上
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = sdf.format(new Date());
		String htmlText = MessageFormat.format(buffer.toString(), contentText, date);
		return htmlText;
	}
}




